//
//  ConditionView.swift
//  MakeMoney
//
//  Created by Fan ZhiQiang on 2024/12/17.
//

import SwiftUI

struct ConditionView: View {
    let year: Int
    @Bindable var pathStore: PathStore
    @State private var startYear: Int
    @State private var endYear: Int
    
    @State private var firstDigits: Int = 0
    @State private var secondDigits: Int = 0
    @State private var thirdDigits: Int = 0
    @State private var forthDigits: Int = 0
    @State private var fifthDigits: Int = 0
    
    @State private var useBall: Int = 0
    
    private var endYears: [Int] {
        lotteryYears.filter { inyear in
            inyear >= startYear
        }
    }
    
    var body: some View {
        List {
            Section {
                Picker("请选择开始年份", selection: $startYear) {
                    ForEach(lotteryYears, id: \.self) { year in
                        Text(String(year) + "年")
                    }
                }
                Picker("请选择结束年份", selection: $endYear) {
                    ForEach(endYears, id: \.self) { year in
                        Text(String(year) + "年")
                    }
                }
            }
            
            Section {
                Picker("请选择第一个球号码", selection: $firstDigits) {
                    ForEach(0..<36) { digits in
                        Text(digits == 0 ? "不选择" : String(digits))
                    }
                }
                
                Picker("请选择第二个球号码", selection: $secondDigits) {
                    ForEach(0..<36) { digits in
                        Text(digits == 0 ? "不选择" : String(digits))
                    }
                }
                
                Picker("请选择第三个球号码", selection: $thirdDigits) {
                    ForEach(0..<36) { digits in
                        Text(digits == 0 ? "不选择" : String(digits))
                    }
                }
                
                Picker("请选择第四个球号码", selection: $forthDigits) {
                    ForEach(0..<36) { digits in
                        Text(digits == 0 ? "不选择" : String(digits))
                    }
                }
                
                Picker("请选择第五个球号码", selection: $fifthDigits) {
                    ForEach(0..<36) { digits in
                        Text(digits == 0 ? "不选择" : String(digits))
                    }
                }
            }
            
            Section {
                Picker("使用第几套开奖球", selection: $useBall) {
                    ForEach(0..<4) { digits in
                        Text(digits == 0 ? "不选择" : String(digits))
                    }
                }
            }
        }
        .toolbar {
            ToolbarItem(placement: .topBarTrailing) {
                Button("查询") {
                    pathStore.path.append(PathName())
                }
            }
        }
        .navigationTitle("选项")
        .navigationDestination(for: PathName.self) { path in
            Text(path.name)
        }
    }
    
    init(year: Int, pathStore: PathStore) {
        self.year = year
        self.pathStore = pathStore
        _startYear = State(initialValue: year)
        _endYear = State(initialValue: year)
    }
    
}

#Preview {
    ConditionView(year: 2024, pathStore: .init())
}


